#include "RedBat.h"
#include "Resource.h"


/**--------------------------------------------------------
*Description : Constructor
*Method name : BatBoss
*Parameters : d3ddv, BackBuffer, SpriteHandler, x, y, ID, Object_State, Direction
*Return value : 
--------------------------------------------------------**/
RedBat::RedBat(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction): Object(x, y)//, ID, Object_State)
{
	this->setIDObjType(ID);	

	pos_x = x;
	pos_y = y;

	this->pos_x_old = x;
	this->pos_y_old = y;

	min_y = pos_y - REDBAT_DISTANCE_Y;
	max_y = pos_y + REDBAT_DISTANCE_Y;
	
	veloc_x = 0;
	veloc_y = 0;
	accel_y = 0;

	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[17]->getFileName()), 125, 0);
	p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);

	//Trang thai unactive
	p_sprite->setFrame(0,1);
	p_sprite->setIndex(0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	time_die = 0;	

	is_turn_left = false;
	

	this->obj_state = Object_State;
	
	direction = Direction;
	if (obj_state == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{	
			is_turn_left = true;
		} 
		else if (direction == 2)	//sang phai
		{			
			is_turn_left = false;
		}
		changeState(ES_ACTIVING);
	} 

	
}

/**--------------------------------------------------------
*Description : Destructor
*Method name : BatBoss
*Parameters : 
*Return value : 
--------------------------------------------------------**/
RedBat::~RedBat()
{
	/*if (_sprite != NULL)
	delete _sprite;*/	
}

 
/**--------------------------------------------------------
*Description : render obj
*Method name : render
*Parameters : cameraX, cameraY
*Return value : void
--------------------------------------------------------**/
void RedBat::render(int cameraX, int cameraY)
{	
	if (obj_state != ES_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}	
}


/**--------------------------------------------------------
*Description : Get obj Type
*Method name : getObjectType
*Parameters : 
*Return value : OBJECT_TYPE
--------------------------------------------------------**/
OBJECT_TYPE RedBat::getObjectType()
{
	return OBJECT_ENEMY;
}


/**--------------------------------------------------------
*Description : update
*Method name : update
*Parameters : obj_list, input, gameTime, simon  
*Return value : void
--------------------------------------------------------**/
void RedBat::update(list<Object*>* obj_list, DxInput* input, float gameTime, Object* simon)
{
	switch (obj_state)
	{
	case  ES_NORMAL:
		if (abs(simon->getPosY() - this->pos_y) < 10)
		{
			changeState(ES_ACTIVING);
		}
		break;

	case ES_ACTIVING:
		move(obj_list, gameTime, simon);		
		break;

	case ES_DIED:
		time_die += gameTime;
		if (time_die > 1200)
		{
			changeState(ES_REMOVE);
		}
		break;
	
	}

	p_sprite->update(gameTime);
}

 
/**--------------------------------------------------------
*Description : move obj
*Method name : move
*Parameters : obj_list, TPF, simon  
*Return value : void
--------------------------------------------------------**/
void RedBat::move(list<Object*>* obj_list, float TPF, Object* simon)
{
	Object* obj;
	list<Object*>::iterator i;
	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();
	
	pos_x_old = pos_x;
	pos_y_old = pos_y;

	pos_x += veloc_x * TPF;
	pos_y += veloc_y * TPF;

	if (pos_y <= min_y || pos_y >= max_y)
	{
		veloc_y *= -1;
	}


	

	//va cham voi simon
	//Box box(this->pos_x_old,
	//	this->pos_y_old - this->height, 
	//	width, 
	//	height, 
	//	veloc_x * TPF, 
	//	veloc_y * TPF);

	//Box block(simon->getPosX(),
	//	simon->getPosY() - simon->getHeight(),
	//	simon->getWidth(),
	//	simon->getHeight()/2
	//	);

	//Box boxphase = Collision::GetSweptBroadphaseBox(box);
	//if (Collision::AABBCheck(boxphase, block))
	//{
	//	changeState(ES_DIED);	//red bat die
	//	if (simon->getObjState() == MS_NORMAL)
	//	{
	//		int blood = simon->getBlood();
	//		blood--;
	//		simon->setBlood(blood);
	//		if (simon->getBlood() == 0)
	//		{
	//			simon->changeState(MS_DIE);		//simon die
	//		}
	//		else
	//		{
	//			simon->changeState(MS_PROTECT);	//simon protected
	//		}
	//	}
	//}


	pos_x_old = pos_x;
	pos_y_old = pos_y;

	if (veloc_x >= 0)
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}


/**--------------------------------------------------------
*Description : change state
*Method name : changeState
*Parameters : state
*Return value : void
--------------------------------------------------------**/
void RedBat::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case ES_ACTIVING:
		veloc_y = REDBAT_VY;
		if (is_turn_left == true)
		{
			veloc_x = -REDBAT_VX;
			p_sprite->setFrame(1,4);
			p_sprite->setIndex(1);
		}
		else
		{
			veloc_x = REDBAT_VX;
			p_sprite->setFrame(5,8);
			p_sprite->setIndex(5);
		}		
		break;
 
	case ES_DIED:		 
		p_sprite = p_light;
		veloc_x = 0;
		break;

	case ES_REMOVE:
		break;
	}
}
